import { BaseItem } from "../common/BaseItem";
import { IClean } from "../../../../com/gengine/core/IClean";
import { ToolTipInfo } from "../../model/ToolTipInfo";
import { NormalTooltip } from "../common/tooltip/NormalTooltip";
import { BuffInfo } from "./BuffInfo";
import { SecTimer } from "../../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../../com/gengine/core/frame/TimerType";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { ImageInfo } from "../../../../com/gengine/resource/info/ImageInfo";
//class BuffItem
    
    export  class BuffItem extends BaseItem implements IClean
    {
        constructor()
        {
            super();
            this.setSize(17, 17);
            this.init();
            return;
        }

        public init(): void
        {
            return;
        }

        public /* override */ get toolTipData()
        {
            if (this._buffInfo && this._buffInfo.tstate) 
            {
                return new ToolTipInfo(NormalTooltip.ToolTipRenderType_BUFF, this._buffInfo);
            }
            return null;
        }

        public get buffInfo(): BuffInfo
        {
            return this._buffInfo;
        }

        public set buffInfo(arg1: BuffInfo)
        {
            this._buffInfo = arg1;
            if (arg1) 
            {
                if (arg1.tstate.category == 1) 
                {
                    if (this._secTimer == null) 
                    {
                        this._secTimer = new SecTimer(1);
                    }
                    this._leftSeconds = arg1.getLeavSeconds();
                    this._secTimer.repeatCount = this._leftSeconds;
                    this._secTimer.addListener(TimerType.ENTERFRAME, this.updateRemainTimeHandler);
                    this._secTimer.addListener(TimerType.COMPLETE, this.secTimeCompleteHandler);
                    this._secTimer.start();
                    Dispatcher.dispatchEvent(new DataEvent(EventName.BuffRemainTimeUpdate, arg1));
                }
                this._isClear = false;
                this.source = arg1.getIconPath();
            }
            else 
            {
                this._isClear = true;
                this._bitmap.bitmapData = null;
            }
            return;
        }

        protected /* override */ onLoadCompleteHandler(arg1: ImageInfo): void
        {
            if (this._isClear == false) 
            {
                super.onLoadCompleteHandler(arg1);
            }
            return;
        }

        /* internal  */updateRemainTimeHandler(arg1: SecTimer): void
        {
            if (this._buffInfo) 
            {
                var loc1;
                var loc2;
                this._buffInfo.setLeavSeconds(loc1._leftSeconds = loc2 = ((loc1 = this)._leftSeconds - 1));
            }
            return;
        }

        /* internal  */secTimeCompleteHandler(arg1: SecTimer): void
        {
            if (this._secTimer) 
            {
                this._secTimer.dispose();
                this._secTimer = null;
            }
            return;
        }

        public clean(): void
        {
            if (this._secTimer) 
            {
                this._secTimer.dispose();
            }
            return;
        }

        /* internal */ /* var */_secTimer: SecTimer;

        /* internal */ /* var */_leftSeconds: number=0;

        /* internal */ /* var */_calBuffInfo: BuffInfo;

        /* internal */ /* var */_buffInfo: BuffInfo;

        /* internal */ /* var */_isClear: Boolean;
    }
